package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * ...
	 * @author Chen Ran
	 */
	public class XMLTest extends Sprite 
	{
		
		public function XMLTest():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			// new XML() is a special object
			// actually it is an empty XML
			// you can not do almost of handle of it
			// such as appendChild, setName...
			
			// comment is a child node if xml don't ignore comments
			//testComments( true );
			
			//testNodeName();
			
			// remeber attribute IS a XML
			//testAttributes();
			
			//oneLineChange();
			
			// you can use operator (==,!=) to compare value of xml
			// use(===, !==) to compare reference of xml
			//compareXML();
			
			// see: http://www.cnblogs.com/holybozo/archive/2011/04/04/2005494.html
			//testE4X();
			
			//testNamespace();
		}
		
		private function testNamespace():void
		{
			var xml:XML = new XML('<?xml version="1.0"?><root xmlns:mx="http://www.w3.org/1999/xhtml"><mx:XML source="a1.xml" id="license"/></root>');
			trace(xml);
		}
		
		private function testE4X():void 
		{
			var xml:XML = <root>
							<node>
								<abc>123</abc>
								<abc>456</abc>
							</node>
							<node>
								<a-b-c>123</a-b-c>
								<abc>456</abc>
							</node>
							<node>
								<abc>123</abc>
								<a-b-c>456</a-b-c>
							</node>
						</root>;
			
			//var abc:String = 'abc';
			var result:XMLList = xml.node.(child('abc') == '123');
			trace('result:', result);
		}
		
		private function compareXML():void 
		{
			var xml1:XML = <root><abc>123</abc></root>;
			var xml2:XML = <root><abc>123</abc></root>;
			
			trace('xml1:\n', xml1.toXMLString());
			trace('xml2:\n', xml2.toXMLString());
			trace('xml1==xml2 is', xml1==xml2);
			trace('xml1!=xml2 is', xml1!=xml2);
			trace('xml1===xml2 is', xml1===xml2);
			trace('xml1!==xml2 is', xml1!==xml2);
		}
		
		private function oneLineChange():void 
		{
			var source:XML = <source><child a='aa'/><child a='aa'/><hah/></source>;
			var dest:XML = <dest/>.appendChild(source.*);
			delete dest.hah;
			
			trace(source.toXMLString());
			trace(dest.toXMLString());
		}
		
		private function testAttributes():void 
		{
			var xml:XML = <root a="aa" b='bb' c='cc'><child p="pp"/></root>;
			//var atts:XMLList = xml.attributes();
			var atts:XMLList = xml.@*;
			trace("==FOR LOOP");
			for (var i:int = 0; i < atts.length(); ++i)
			{
				trace( atts[i] );
			}
			trace("==FORIN LOOP");
			for (var key:String in atts)
			{
				trace('key:', key, typeof(key),'val:', atts[key], typeof(atts[key]));
			}
			trace("==FOREACH LOOP");
			for each( var att:XML in atts )
			{
				trace('att:', att.name(), att.toString());
			}
			
			// modify the attribute directly
			trace(atts[0] = 'tt');
			
			trace(xml.toXMLString());
		}
		
		private function testNodeName():void 
		{
			var source:XML = <root><child prop="1"/></root>;
			var dest:XML = <dest/>;
			
			dest.setName( source.name() );
			
			trace('source.name()', source.name(), typeof(source.name()));
			trace(dest.toXMLString());
		}
		
		private function testComments( trueOrFalse:Boolean ):void 
		{
			XML.ignoreComments = trueOrFalse;
			
			var xml:XML = <root><child1/><!-- test comments --><child2/></root>;
			trace(xml.toXMLString());
			trace(xml.children().toXMLString());
			trace(xml.children().length());
			
			xml.appendChild( <!-- test2 --> );
			trace(xml.toXMLString());
			trace(xml.children().length());
		}
		
	}
	
}